package action;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Map;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.validation.SkipValidation;
import org.hibernate.classic.Session;

import pojo.Appuser;
import pojo.Watchuser;
import service.UserService;
import service.impl.UserServiceImpl;





import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.validator.annotations.Validation;

public class UserAction extends ActionSupport implements ModelDriven<Appuser>{
	

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Appuser appuser = new Appuser();
	private UserService userService =new UserServiceImpl();
	private String username;
	private String pwd;


	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	Map<String, Object> session = ActionContext.getContext().getSession();
	

	

	//登录
	public String login(){
		//每次登陆之前先去数据库查询appuser中的binder字段有没有数据
		 appuser = userService.login(appuser);
		if(appuser!=null){
			session.put("appuser", appuser);
			return "login_success";
			
		}else{
			return "login_failure";
		}
		
	}
    private String chknumber;  
    
	public String getChknumber() {
		return chknumber;
	}


	public void setChknumber(String chknumber) {
		this.chknumber = chknumber;
	}


	//校验
	public void validate(){
		String regex = "1[3|4|5|7|8|][0-9]{9}";
		//String regex2 = "[\u4e00-\u9fa5]{1}[A-Z]{1}[A-Z_0-9]{5}";
		if("".equals(appuser.getUsername().trim()) || ! appuser.getUsername().matches(regex)){
			this.addFieldError("usernameError", "请输入有效的手机号码");
		}
		
		if(appuser.getPwd().length()<3 || appuser.getPwd().length()>8){
			this.addFieldError("pwdError", "密码为6到12位的非汉字符");
		}
	/*	if("".equals(appuser.getUsername().trim())){
			this.addFieldError("usernameError", "用户名不能为空");
		}
		if(appuser.getPwd().length()<3 || appuser.getPwd().length()>8){
			this.addFieldError("pwdError", "密码为3到8位的非汉字符");
		}*/
		if(!chknumber.equals(session.get("piccode"))){
			this.addFieldError("numberError", "验证码不正确");
		}
		
		
	}
	//退出
	@SkipValidation
		public String logOut(){
			if(session.get("appuser")!=null){
				session.remove("appuser");	
			}
			return "logOut_success";

		}

	@Override
	public Appuser getModel() {
		// TODO Auto-generated method stub
		return appuser;
	}



}
